using AForge.Imaging;
using AForge.Video;
using AForge.Video.DirectShow;
using Declarations;
using Declarations.Filters;
using Declarations.Media;
using Declarations.Players;
using Implementation;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Threading;
using System.Timers;
using System.Windows.Forms;
namespace SilverSea.Video.Tools
{
	public class VLCPlayer : UserControl, IVideoSourcePlayer
	{
		private IMediaPlayerFactory imediaPlayerFactory_0;
		private IVideoPlayer ivideoPlayer_0;
		private IMedia imedia_0;
		private IAdjustFilter iadjustFilter_0;
		private Thread thread_0;
		private IVideoSource ivideoSource_0;
		private string string_0 = string.Empty;
		private TextOverlayLocation textOverlayLocation_0 = TextOverlayLocation.BottomLeft;
		private System.Drawing.Color color_0 = System.Drawing.Color.Blue;
		private System.Drawing.Color color_1 = System.Drawing.Color.Black;
		private System.Drawing.Color color_2 = System.Drawing.Color.Green;
		private int int_0 = 1;
		private System.Drawing.Rectangle[] rectangle_0;
		private VideoSourceErrorEventHandler videoSourceErrorEventHandler_0;
		private VideoWriterFinishEventHandler videoWriterFinishEventHandler_0;
		private DateTime dateTime_0 = DateTime.Now;
        //private bool bool_0;
        //private bool bool_1;
		private System.Timers.Timer timer_0 = new System.Timers.Timer();
		private string string_1 = string.Empty;
		private System.Timers.Timer timer_1 = new System.Timers.Timer();
        //private int int_1;
		private object object_0 = new object();
        private float float_0;
		private float float_1;
		private bool bool_2;
		private int int_2 = 5;
		private string string_2 = "div4";
		private int int_3 = 30;
		public string videoFolder = string.Empty;
		private NewFrameHandler newFrameHandler_0;
		private bool bool_3 = true;
		private string string_3 = "Deinterlace Filter";
		private IContainer icontainer_0;
		private Panel panel1;
		public event VideoSourceErrorEventHandler VideoSourceError
		{
			add
			{
				VideoSourceErrorEventHandler videoSourceErrorEventHandler = this.videoSourceErrorEventHandler_0;
				VideoSourceErrorEventHandler videoSourceErrorEventHandler2;
				do
				{
					videoSourceErrorEventHandler2 = videoSourceErrorEventHandler;
					VideoSourceErrorEventHandler value2 = (VideoSourceErrorEventHandler)Delegate.Combine(videoSourceErrorEventHandler2, value);
					videoSourceErrorEventHandler = Interlocked.CompareExchange<VideoSourceErrorEventHandler>(ref this.videoSourceErrorEventHandler_0, value2, videoSourceErrorEventHandler2);
				}
				while (videoSourceErrorEventHandler != videoSourceErrorEventHandler2);
			}
			remove
			{
				VideoSourceErrorEventHandler videoSourceErrorEventHandler = this.videoSourceErrorEventHandler_0;
				VideoSourceErrorEventHandler videoSourceErrorEventHandler2;
				do
				{
					videoSourceErrorEventHandler2 = videoSourceErrorEventHandler;
					VideoSourceErrorEventHandler value2 = (VideoSourceErrorEventHandler)Delegate.Remove(videoSourceErrorEventHandler2, value);
					videoSourceErrorEventHandler = Interlocked.CompareExchange<VideoSourceErrorEventHandler>(ref this.videoSourceErrorEventHandler_0, value2, videoSourceErrorEventHandler2);
				}
				while (videoSourceErrorEventHandler != videoSourceErrorEventHandler2);
			}
		}
		public event VideoWriterFinishEventHandler VideoWriterFinish
		{
			add
			{
				VideoWriterFinishEventHandler videoWriterFinishEventHandler = this.videoWriterFinishEventHandler_0;
				VideoWriterFinishEventHandler videoWriterFinishEventHandler2;
				do
				{
					videoWriterFinishEventHandler2 = videoWriterFinishEventHandler;
					VideoWriterFinishEventHandler value2 = (VideoWriterFinishEventHandler)Delegate.Combine(videoWriterFinishEventHandler2, value);
					videoWriterFinishEventHandler = Interlocked.CompareExchange<VideoWriterFinishEventHandler>(ref this.videoWriterFinishEventHandler_0, value2, videoWriterFinishEventHandler2);
				}
				while (videoWriterFinishEventHandler != videoWriterFinishEventHandler2);
			}
			remove
			{
				VideoWriterFinishEventHandler videoWriterFinishEventHandler = this.videoWriterFinishEventHandler_0;
				VideoWriterFinishEventHandler videoWriterFinishEventHandler2;
				do
				{
					videoWriterFinishEventHandler2 = videoWriterFinishEventHandler;
					VideoWriterFinishEventHandler value2 = (VideoWriterFinishEventHandler)Delegate.Remove(videoWriterFinishEventHandler2, value);
					videoWriterFinishEventHandler = Interlocked.CompareExchange<VideoWriterFinishEventHandler>(ref this.videoWriterFinishEventHandler_0, value2, videoWriterFinishEventHandler2);
				}
				while (videoWriterFinishEventHandler != videoWriterFinishEventHandler2);
			}
		}
		public event NewFrameHandler NewFrame
		{
			add
			{
				NewFrameHandler newFrameHandler = this.newFrameHandler_0;
				NewFrameHandler newFrameHandler2;
				do
				{
					newFrameHandler2 = newFrameHandler;
					NewFrameHandler value2 = (NewFrameHandler)Delegate.Combine(newFrameHandler2, value);
					newFrameHandler = Interlocked.CompareExchange<NewFrameHandler>(ref this.newFrameHandler_0, value2, newFrameHandler2);
				}
				while (newFrameHandler != newFrameHandler2);
			}
			remove
			{
				NewFrameHandler newFrameHandler = this.newFrameHandler_0;
				NewFrameHandler newFrameHandler2;
				do
				{
					newFrameHandler2 = newFrameHandler;
					NewFrameHandler value2 = (NewFrameHandler)Delegate.Remove(newFrameHandler2, value);
					newFrameHandler = Interlocked.CompareExchange<NewFrameHandler>(ref this.newFrameHandler_0, value2, newFrameHandler2);
				}
				while (newFrameHandler != newFrameHandler2);
			}
		}
		public IVideoSource VideoSource
		{
			get
			{
				return this.ivideoSource_0;
			}
			set
			{
				this.ivideoSource_0 = value;
			}
		}
		[DefaultValue(typeof(System.Drawing.Color), "Black")]
		public System.Drawing.Color BorderColor
		{
			get
			{
				return this.color_1;
			}
			set
			{
				this.color_1 = value;
				this.BackColor = this.color_1;
				base.Invalidate();
			}
		}
		[DefaultValue(typeof(System.Drawing.Color), "Green")]
		public System.Drawing.Color MotionRectangleColor
		{
			get
			{
				return this.color_2;
			}
			set
			{
				this.color_2 = value;
				base.Invalidate();
			}
		}
		[DefaultValue(1)]
		public int MotionRectangleLineWidth
		{
			get
			{
				return this.int_0;
			}
			set
			{
				this.int_0 = value;
				base.Invalidate();
			}
		}
		public System.Drawing.Rectangle[] MotionRectangles
		{
			get
			{
				return this.rectangle_0;
			}
			set
			{
				this.rectangle_0 = value;
			}
		}
		public string TextOverlay
		{
			get
			{
				return this.string_0;
			}
			set
			{
				this.string_0 = value;
			}
		}
		public TextOverlayLocation TextOverlayLocation
		{
			get
			{
				return this.textOverlayLocation_0;
			}
			set
			{
				this.textOverlayLocation_0 = value;
			}
		}
		public System.Drawing.Color TextOverlayColor
		{
			get
			{
				return this.color_0;
			}
			set
			{
				this.color_0 = value;
			}
		}
		public DateTime LastTimeReceivedFrame
		{
			get
			{
				return this.dateTime_0;
			}
			set
			{
				this.dateTime_0 = value;
			}
		}
		public float FPS
		{
			get
			{
				return this.float_0;
			}
		}
		public float BPS
		{
			get
			{
				return this.float_1;
			}
		}
		public bool EnableRecording
		{
			get
			{
				return this.bool_2;
			}
			set
			{
				this.bool_2 = value;
			}
		}
		public int RecordingRate
		{
			get
			{
				return this.int_2;
			}
			set
			{
				this.int_2 = value;
			}
		}
		public string RecordingCodec
		{
			get
			{
				return this.string_2;
			}
			set
			{
				this.string_2 = value;
			}
		}
		public int VideoLengthLimit
		{
			get
			{
				return this.int_3;
			}
			set
			{
				this.int_3 = value;
			}
		}
		public string VideoFolder
		{
			get
			{
				return this.videoFolder;
			}
			set
			{
				this.videoFolder = value;
			}
		}
		public bool UsingDeinterlace
		{
			get
			{
				return this.bool_3;
			}
			set
			{
				this.bool_3 = value;
			}
		}
		public string DeinterlaceFilter
		{
			get
			{
				return this.string_3;
			}
			set
			{
				this.string_3 = value;
			}
		}
		public bool EnableAdjustFilter
		{
			set
			{
				lock (this.object_0)
				{
					if (this.ivideoPlayer_0 != null && value)
					{
                        this.iadjustFilter_0 = this.ivideoPlayer_0.Adjust;
					}
					else
					{
						this.iadjustFilter_0 = null;
					}
				}
			}
		}
		public float Brightness
		{
			get
			{
				if (this.iadjustFilter_0 != null)
				{
                    return this.iadjustFilter_0.Brightness;
				}
				return 0f;
			}
			set
			{
				lock (this.object_0)
				{
					if (this.iadjustFilter_0 != null)
					{
                        this.iadjustFilter_0.Enabled = true;
						this.iadjustFilter_0.Brightness = value;
					}
				}
			}
		}
		public float Contrast
		{
			get
			{
				float result = 0f;
				lock (this.object_0)
				{
					if (this.iadjustFilter_0 != null)
					{
                        result = this.iadjustFilter_0.Contrast;
					}
				}
				return result;
			}
			set
			{
				lock (this.object_0)
				{
					if (this.iadjustFilter_0 != null)
					{
						this.iadjustFilter_0.Enabled = true;
						this.iadjustFilter_0.Contrast = value;
					}
				}
			}
		}
		public float Gamma
		{
			get
			{
				float result = 0f;
				lock (this.object_0)
				{
					if (this.iadjustFilter_0 != null)
					{
                        result = this.iadjustFilter_0.Gamma;
					}
				}
				return result;
			}
			set
			{
				lock (this.object_0)
				{
					if (this.iadjustFilter_0 != null)
					{
                        this.iadjustFilter_0.Enabled = true;
                        this.iadjustFilter_0.Gamma = value;
					}
				}
			}
		}
		public bool IsRunning
		{
			get
			{
				bool result = false;
				lock (this.object_0)
				{
					if (this.ivideoPlayer_0 != null)
					{
                        result = this.ivideoPlayer_0.IsPlaying;
					}
				}
				return result;
			}
		}
		public VLCPlayer()
		{
			this.InitializeComponent();
		}
		public System.Drawing.Bitmap GetCurrentVideoFrame()
		{
			return this.method_1();
		}
		public UnmanagedImage GetCurrentUnmanagedVideoFrame()
		{
			System.Drawing.Bitmap bitmap = this.method_1();
			if (bitmap != null)
			{
				return UnmanagedImage.FromManagedImage(bitmap);
			}
			return null;
		}
		public void Start()
		{
            try
            {
                this.method_0();
                lock (this.object_0)
                {
                    this.imediaPlayerFactory_0 = new MediaPlayerFactory();
                    this.ivideoPlayer_0 = this.imediaPlayerFactory_0.CreatePlayer<IVideoPlayer>();
                    if (this.ivideoPlayer_0 == null)
                    {
                        return;
                    }
                    this.ivideoPlayer_0.AspectRatio = 0;
                    this.ivideoPlayer_0.WindowHandle = this.panel1.Handle;
                    List<string> list = new List<string>();
                    if (!(this.ivideoSource_0 is VLC_VideoCaptureDevice))
                    {
                        goto Label_01FF;
                    }
                    VLC_VideoCaptureDevice device = (VLC_VideoCaptureDevice)this.ivideoSource_0;
                    string str = string.Empty;
                    FilterInfoCollection infos = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                    int num = 0;
                    while (num < infos.Count)
                    {
                        if (infos[num].MonikerString == device.Source)
                        {
                            goto Label_00C6;
                        }
                        num++;
                    }
                    goto Label_00D4;
                Label_00C6:
                    str = infos[num].Name;
                Label_00D4:
                    if (device.Chanel > 0)
                    {
                        str = str + " #" + device.Chanel;
                    }
                    int width = 640;
                    int height = 480;
                    if ((device.DesiredFrameSize.Width > 0) && (device.DesiredFrameSize.Height > 0))
                    {
                        width = device.DesiredFrameSize.Width;
                        height = device.DesiredFrameSize.Height;
                    }
                    string str2 = width + "x" + height;
                    int desiredFrameRate = 0x19;
                    if ((device.DesiredFrameRate > 0) && (device.DesiredFrameRate <= 0x19))
                    {
                        desiredFrameRate = device.DesiredFrameRate;
                    }
                    this.bool_3 = device.UsingDeInterlace;
                    this.string_3 = device.DeinterlaceFilter;
                    this.imedia_0 = this.imediaPlayerFactory_0.CreateMedia<IMedia>("dshow://", new string[] { "dshow-vdev=" + str, "dshow-adev=none", "dshow-size=" + str2, "dshow-fps=" + desiredFrameRate, "dshow-caching=10" });
                Label_01FF:
                    list.Add("no-snapshot-preview");
                    list.Add("snapshot-format=jpg");
                    if (this.imedia_0 != null)
                    {
                        if (list.Count > 0)
                        {
                            this.imedia_0.AddOptions(list);
                        }
                        this.ivideoPlayer_0.Open(this.imedia_0);
                        this.imedia_0.Parse(true);
                        this.ivideoPlayer_0.Play();
                        this.ivideoPlayer_0.Volume = 0;
                        if (this.bool_3)
                        {
                            DeinterlaceMode mode;
                            if (this.string_3 == "bob")
                            {
                                mode = DeinterlaceMode.bob;
                            }
                            else
                            {
                                if (this.string_3 == "discard")
                                {
                                    mode = DeinterlaceMode.discard;
                                }
                                else
                                {
                                    if (this.string_3 == "linear")
                                    {
                                        mode = DeinterlaceMode.linear;
                                    }
                                    else
                                    {
                                        if (this.string_3 == "mean")
                                        {
                                            mode = DeinterlaceMode.mean;
                                        }
                                        else
                                        {
                                            if (this.string_3 == "x")
                                            {
                                                mode = DeinterlaceMode.x;
                                            }
                                            else
                                            {
                                                if (this.string_3 == "yadif")
                                                {
                                                    mode = DeinterlaceMode.yadif;
                                                }
                                                else
                                                {
                                                    if (this.string_3 == "yadif2x")
                                                    {
                                                        mode = DeinterlaceMode.yadif2x;
                                                    }
                                                    else
                                                    {
                                                        mode = DeinterlaceMode.blend;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            this.ivideoPlayer_0.Deinterlace.Mode = mode;
                            this.ivideoPlayer_0.Deinterlace.Enabled = true;
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                if (this.videoSourceErrorEventHandler_0 != null)
                {
                    this.videoSourceErrorEventHandler_0.Invoke(this, new VideoSourceErrorEventArgs("VLC start video source: " + exception.Message));
                }
            }
		}
		public void SignalToStop()
		{
			lock (this.object_0)
			{
				if (this.thread_0 == null)
				{
					this.thread_0 = new Thread(new ThreadStart(this.WorkerThread));
					this.thread_0.IsBackground = true;
                    this.thread_0.Name = this.ivideoSource_0.Source;
					this.thread_0.Start();
				}
			}
		}
		public void Stop()
		{
			lock (this.object_0)
			{
				if (this.thread_0 != null)
				{
					this.thread_0.Abort();
					this.thread_0 = null;
				}
			}
			Application.DoEvents();
			this.method_0();
		}
		public void WorkerThread()
		{
			lock (this.object_0)
			{
				IVideoPlayer videoPlayer = this.ivideoPlayer_0;
				if (videoPlayer != null)
				{
					videoPlayer.Stop();
				}
				Application.DoEvents();
			}
		}
		private void method_0()
		{
			lock (this.object_0)
			{
				if (this.imediaPlayerFactory_0 != null)
				{
					this.imediaPlayerFactory_0.Dispose();
					this.imediaPlayerFactory_0 = null;
				}
				Application.DoEvents();
				if (this.ivideoPlayer_0 != null)
				{
					this.ivideoPlayer_0.Dispose();
					this.ivideoPlayer_0 = null;
				}
				Application.DoEvents();
				if (this.imedia_0 != null)
				{
					this.imedia_0.Dispose();
					this.imedia_0 = null;
				}
				Application.DoEvents();
			}
		}
		public void StartRecording()
		{
		}
		public void StopRecording()
		{
		}
		public void ToggleMute()
		{
			lock (this.object_0)
			{
				if (this.ivideoPlayer_0 != null)
				{
					this.ivideoPlayer_0.ToggleMute();
				}
			}
		}
		public void SaveCurrentVideoFrame(string path)
		{
			lock (this.object_0)
			{
				if (this.ivideoPlayer_0 != null)
				{
					this.ivideoPlayer_0.TakeSnapShot(0u, path);
				}
			}
		}
		private System.Drawing.Bitmap method_1()
		{
			return null;
		}
		protected override void Dispose(bool disposing)
		{
			if (disposing && this.icontainer_0 != null)
			{
				this.icontainer_0.Dispose();
			}
			base.Dispose(disposing);
		}
		private void InitializeComponent()
		{
			this.panel1 = new Panel();
			base.SuspendLayout();
			this.panel1.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right);
			this.panel1.Enabled = false;
			this.panel1.Location = new System.Drawing.Point(2, 2);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(146, 146);
			this.panel1.TabIndex = 0;
			base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
//			base.AutoScaleMode = AutoScaleMode.Font;
			base.Controls.Add(this.panel1);
			base.Name = "VLCPlayer";
			base.ResumeLayout(false);
		}
	}
}
